package com.febers.ihealth

import android.app.ProgressDialog
import android.content.Intent
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.febers.ihealth.base.BaseActivity
import com.febers.ihealth.utils.IOUtil
import com.febers.ihealth.utils.nav
import com.febers.ihealth.utils.user
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.toolbar_common.*

class LoginActivity : BaseActivity() {

    private var progressDialog: ProgressDialog? = null
    private var signInStatus = false

    override fun setView(): Int = R.layout.activity_login

    override fun setToolbar(): Toolbar? = toolbar_common

    override fun setToolbarTitle(): String? = "登录"

    override fun initView() {
        signInStatus = intent.getBooleanExtra("sign_in", false)

        if (signInStatus) {
            setToolbar()?.title = "注册"
            btn_login.text = "注册"
            btn_sign_in.visibility = View.GONE
        } else {
            setToolbar()?.title = "登录"
            btn_login.text = "登录"
            btn_sign_in.visibility = View.VISIBLE
            btn_sign_in.setOnClickListener {
                startActivity(Intent(this, LoginActivity::class.java).apply { putExtra("sign_in", true) })
                finish()
            }
        }
        btn_login.setOnClickListener {
            onLoginOrSignIn(edit_text_login_name.text.toString(), edit_text_login_pw.text.toString())
        }
//        user.name = "测试账号"
//        if (user.name.isNotEmpty()) {
//            nav(NavActivity::class.java)
//        }
    }

    override fun afterCreate() {
        val u = IOUtil.readUser()
        if (u.registered()) {
            user = u
            progressDialog().setTitle("您已登录，正在跳转")
            progressDialog().show()
            btn_login.postDelayed({
                nav(NavActivity::class.java)
                progressDialog().dismiss()
                finish()
            }, 500)
        }
    }

    private fun onLoginOrSignIn(userName: String, userPw: String) {
        if (userName.isEmpty()) {
            edit_text_login_name.error = "用户名不能为空"
            return
        }
        if (userPw.isEmpty()) {
            edit_text_login_pw.error = "密码不能为空"
            return
        }
        user.name = edit_text_login_name.text.toString()
        user.pw = edit_text_login_pw.text.toString()
        IOUtil.saveUser(user)
        if (signInStatus) {
            progressDialog().setTitle("正在注册，请稍后")
            progressDialog().show()
            btn_login.postDelayed({
                progressDialog().setTitle("注册成功，正在跳转")
            }, 500)
            btn_login.postDelayed({
                nav(NavActivity::class.java)
                progressDialog().dismiss()
                finish()
            }, 1000)
        } else {
            progressDialog().setTitle("正在登录，请稍后")
            progressDialog().show()
            btn_login.postDelayed({
                progressDialog().setTitle("登录成功，正在跳转")
            }, 500)
            btn_login.postDelayed({
                nav(NavActivity::class.java)
                progressDialog().dismiss()
                finish()
            }, 1000)
        }
    }

    private fun progressDialog(): ProgressDialog {
        if (progressDialog == null) {
            progressDialog = ProgressDialog(this)
        }
        return progressDialog!!
    }
}
